package com.core.base.searchForm.hql;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.core.base.searchForm.AbstractBaseSearchForm;
import com.core.base.searchForm.annotation.SearchPropHQL;
import com.core.base.searchForm.support.HQLSearchProp;
import com.core.util.ClassUtil;
import com.core.util.CommonUtil;

/**
 * 针对使用HQL进行查询的情况
 * 
 * @author pt-chenqw
 *
 */
public abstract class BaseHQLSearchform extends AbstractBaseSearchForm
		implements IBaseHQLSearchForm {

	@Override
	public List<HQLSearchProp> getPropsHQL() {
		return getPropsHQLByFields();
	}
	
	@Override
	public void setCurrentPage(int currentPage) {
		super.setCurrentPage(currentPage);
	}

	/**
	 * 根据注解解析字段和值
	 * @return
	 */
	private List<HQLSearchProp> getPropsHQLByFields(){
		List<HQLSearchProp> hqlSearchProps = new ArrayList<>();
		List<Field> fields = ClassUtil.getAllDeclaredFields(getClass());
		for (Field field : fields) {
			SearchPropHQL searchPropHQL = field.getAnnotation(SearchPropHQL.class);
			if(searchPropHQL != null){
				Object fieldValue = ClassUtil.getFieldValue(this, field);
				String paramName = "";
				if(!CommonUtil.isNullOrDefaultValue(fieldValue, searchPropHQL.nullValue())){
					if("".equals(searchPropHQL.paramName())){
						paramName = field.getName();
					}else{
						paramName = searchPropHQL.paramName();
					}
					HQLSearchProp hqlSearchProp = new HQLSearchProp(paramName, fieldValue, searchPropHQL.nullValue(), searchPropHQL.searchType());
					hqlSearchProps.add(hqlSearchProp);
				}
			}
		}
		return hqlSearchProps;
	}
}
